#include "vtcnet.h"
#include <stdio.h>
#include <stdlib.h>

BeatNotifier::BeatNotifier(ProductInfo* vtcProdInfo)
{
    m_fd = socket(AF_INET, SOCK_DGRAM, 0);

    int op = 1;
    setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&op, sizeof(op));

    m_beatAddr.sin_family = AF_INET;
    m_beatAddr.sin_port = htons(BEAT_PORT);
    m_beatAddr.sin_addr.S_un.S_addr = htonl(INADDR_BROADCAST);
    //m_beatAddr.sin_addr.S_un.S_addr = INADDR_ANY;
    //m_beatAddr.sin_addr.S_un.S_addr = NetContext::M_host_sockaddr.sin_addr.S_un.S_addr;
    //m_beatAddr.sin_addr.S_un.S_un_b.s_b4 = 255;

    memset(&m_myBeat, 0x00, sizeof(m_myBeat));
    m_myBeat.protoVer = 0x20000200;
    m_myBeat.model = vtcProdInfo->dvModelCode;
    m_myBeat.ver = vtcProdInfo->dvOsver;
    m_myBeat.sn = vtcProdInfo->dvSn;
    strcpy_s(m_myBeat.name, sizeof(m_myBeat.name), vtcProdInfo->device);
    m_myBeat.msgAddr = NetContext::GetLanAddr();
    m_myBeat.msgPort = VTC_MSG_PORT;
}

BeatNotifier::~BeatNotifier()
{
    closesocket(m_fd);
}

void BeatNotifier::SetHotState(WORD hotState, DWORD hotClientAddr)
{
    m_myBeat.hotState = hotState;
    m_myBeat.hotClientAddr = hotClientAddr;
}

BOOL BeatNotifier::DoNotify(void)
{
    int ret = sendto(m_fd, (const char*)(&m_myBeat), sizeof(m_myBeat), 0, (const sockaddr*)(&m_beatAddr), sizeof(m_beatAddr));
    return((ret > 0) ? TRUE:FALSE);
}
